package com.xzcedu.reflection.classload_;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Scanner;

/**
 * @author 徐州城
 * @version 1.0
 */
public class ClassLoad {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {

        Scanner scanner = new Scanner(System.in);
        System.out.print("输入key");
        String key = scanner.next();

        switch (key) {
            case "1" :
                new Dog().cry();//静态加载
                break;
            case "2":
                //反射 静态加载
                Class<?> cls = Class.forName("com.xzcedu.reflection.classload_.Person");
                Object o = cls.newInstance();
                Method method = cls.getMethod("hi");
                method.invoke(o);
                System.out.println("ok");
                break;
            default:
                System.out.println("do nothing");
        }
    }
}

//因为new Dog() 是静态加载，因此必须编写Dog
//Person类是动态加载，所以没有编写Person类也不会报错，只有动态加载该类时，才会报错
class Dog{

    public void cry(){
        System.out.println("小狗 汪汪叫~~~");
    }
}

class Person{
    public void hi(){
        System.out.println("小孩打招呼~~");
    }
}
